<?php

namespace dengje\workerman\process\timing;

use dengje\workerman\process\timing\task\Test;
use Workerman\Worker;
use Workerman\Timer;
use dengje\workerman\Heartbeat;

class Handler
{
	public function onWorkerStart(Worker $worker): void
	{
		//$timer = require_once root_path().DIRECTORY_SEPARATOR.'process'.DIRECTORY_SEPARATOR.'Timer.php';
		Heartbeat::start();
		$className = 'app\Timer';
		// 反射类
		$reflectionClass = new \ReflectionClass($className);

		// 创建类的实例
		$instance = $reflectionClass->newInstance();
		$instance->onWorkerStart();

		foreach (scandir(root_path() . 'plugin') as $key => $value) {
			if (is_dir(root_path() . 'plugin' . DIRECTORY_SEPARATOR . $value)) {
				if ($value != '.' && $value != '..') {
					// 判断有没有定时器文件
					if (file_exists(root_path() . 'plugin' . DIRECTORY_SEPARATOR . $value . DIRECTORY_SEPARATOR . 'Timer.php')) {
						$className = 'plugin\\' . $value . '\\Timer';
						// 反射类
						$reflectionClass = new \ReflectionClass($className);
						// 创建类的实例
						$instance = $reflectionClass->newInstance();
						$instance->onWorkerStart();
					}

				}

			}
		}
	}


	// (new \dengje\workerman\process\timing\Client())->a();
	// 每2.5秒执行一次
	//  $time_interval = 1;
	//  \Workerman\Timer::add($time_interval, function () {
	// 	 echo "task 199run\n";
	// 	 echo "task 11129999run\n";
	//  });
	// event('onTimerWorkerStart');
	// (new \dengje\workerman\process\timing\Client())->setInterval(5)->run(Test::class);



}